<template>
  <!-- 添加或修改通道信息对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
      size="small">
      <el-form-item label="通道代码" prop="channelCode">
        <el-input v-model.trim="form.channelCode" placeholder="请输入通道代码" />
      </el-form-item>
      <el-form-item label="通道名称" prop="channelName">
        <el-input v-model.trim="form.channelName" placeholder="请输入通道名称" />
      </el-form-item>
      <el-form-item label="进出场" prop="ieType">
        <el-select v-model.trim="form.ieType" placeholder="请选择进出场">
          <el-option v-for="dict in dict.type.field_ie_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="进出口" prop="ieFlag">
        <el-select v-model.trim="form.ieFlag" placeholder="请选择进出口">
          <el-option v-for="dict in dict.type.sys_i_e_flag" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="贸易类型" prop="tradeType">
        <el-select v-model.trim="form.tradeType" placeholder="请选择贸易类型">
          <el-option v-for="dict in dict.type.trade_type_list" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="位置类型" prop="gatePosType">
        <el-select v-model.trim="form.gatePosType" placeholder="请选择贸易类型">
          <el-option v-for="dict in dict.type.chnl_gate_pos_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="监管场所" prop="fieldCode">
        <el-select v-model.trim="form.fieldCode" @change="fieldCodeChange" placeholder="请选择监管场所">
         <el-option v-for="dict in selectBaseFieldInfoList" :key="dict.value" :label="dict.label"
           :value="dict.value"></el-option>
       </el-select>
      </el-form-item>

      <el-form-item label="场站名称" prop="fieldName">
        <el-input v-model.trim="form.fieldName" disabled placeholder="请输入场站名称" />
      </el-form-item>
      <el-form-item label="第几道卡" prop="gateLevel">
        <el-input v-model.trim="form.gateLevel" placeholder="请输入第几道卡" />
      </el-form-item>

      <el-form-item label="互市卡口" prop="bmGateType">
        <el-select v-model.trim="form.bmGateType" placeholder="请选择互市卡口">
         <el-option v-for="dict in dict.type.hs_gate_type" :key="dict.value" :label="dict.label"
           :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import selectBaseFieldInfo from "@/views/components/base/selectBaseFieldInfo";
  import {
    getChannelInfo,
    addChannelInfo,
    updateChannelInfo
  } from "@/api/system/channelInfo";
  import {
    findBaseFieldInfo,
    getBaseFieldInfo
  } from "@/api/system/baseFieldInfo";
  export default {
    name: "ChannelInfo-addEdit",
    components: {
      selectBaseFieldInfo,
    },
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        // 表单参数
        form: {
          channelCode: null,
          channelName: null,
          ieType: null,
          ieFlag: null,
          tradeType: null,
          fieldCode: null,
          fieldName: null,
          createTime: null,
          createBy: null,
          remark: null,
          gateLevel: null,
          bmGateType: null,
          gatePosType:null,

        },
        //下来场所列表
        selectBaseFieldInfoList: [],
        // 通道信息表格数据
        channelInfoList: [],
        // 表单校验
        rules: {
          channelCode: [{
            required: true,
            message: "通道编码不能为空",
            trigger: "blur"
          }],
          channelName: [{
            required: true,
            message: "通道名称不能为空",
            trigger: "blur"
          }],
          gatePosType: [{
            required: true,
            message: "位置类型不能为空",
            trigger: "blur"
          }],
          ieType: [{
            required: true,
            message: "进出场不能为空",
            trigger: "change"
          }],
          ieFlag: [{
            required: true,
            message: "进出口不能为空",
            trigger: "change"
          }],
          tradeType: [{
            required: true,
            message: "贸易类型不能为空",
            trigger: "change"
          }],
          fieldCode: [{
            required: true,
            message: "场站编码不能为空",
            trigger: "blur"
          }],
          createTime: [{
            required: true,
            message: "创建时间不能为空",
            trigger: "blur"
          }],
          createBy: [{
            required: true,
            message: "创建人不能为空",
            trigger: "blur"
          }],
          gateLevel: [{
            required: true,
            message: "第几道卡不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    methods: {
      open() {
        this.dialogVisible = true;
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.loadDetail();
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
          this.getLoadBaseFieldInfoList();
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },
      //获取场所下拉列表
      getLoadBaseFieldInfoList() {
        findBaseFieldInfo({
          customsCode: this.userCustomsCode
        }).then(response => {
          this.selectBaseFieldInfoList = response.data;
        })
      },

      // 获取详情
      loadDetail() {
        const channelCode = this.rowData.channelCode;
        getChannelInfo(channelCode).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
          }
        });
      },
      selectResHandler(selectRes){
        this.form.fieldCode=selectRes.fieldCode;
        this.rules.fieldCode[0].required=false;
      },

      fieldCodeChange(val){
        let fin= this.selectBaseFieldInfoList.filter(item=>val==item.value)[0];
        this.form.fieldName=fin.label;
      },

      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            if (this.dialogType == "add") {
              addChannelInfo(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updateChannelInfo(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped></style>
